﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Threading;
// Dave
namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for AddDriver.xaml
    /// </summary>
    public partial class AddDriver : Window
    {
        public AddDriver()
        {
            InitializeComponent();
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            driverAddedSucessfullyLabel.Visibility = System.Windows.Visibility.Hidden;
            driverAlreadyExistsLabel.Visibility = System.Windows.Visibility.Hidden;
            DataClassesDataContext db = new DataClassesDataContext();

            IQueryable<COMSERV_BANNER_STUDENT> students = from c in db.COMSERV_BANNER_STUDENTs
                                                          where c.first_name.Contains(firstnameTextbox.Text) && c.last_name.Contains(lastnameTextbox.Text)
                                                          select c;
            students.ToList(); //executes and puts into list
            displaydataGrid.ItemsSource = students;
        }
        public List<COMSERV_BANNER_STUDENT> students
        {
            get;
            set;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (displaydataGrid.SelectedItem != null)
            {
                firstnameDisplayLabel.Content = ((COMSERV_BANNER_STUDENT)(displaydataGrid.SelectedItem)).first_name;
                lastnameDisplayLabel.Content = ((COMSERV_BANNER_STUDENT)(displaydataGrid.SelectedItem)).last_name;
                emailDisplayTextbox.Text = ((COMSERV_BANNER_STUDENT)(displaydataGrid.SelectedItem)).email;

                DataClassesDataContext db = new DataClassesDataContext();
                IQueryable<COMSERV_MEMBER> student = from c in db.COMSERV_MEMBERs
                                                     where c.bannerid == ((COMSERV_BANNER_STUDENT)(displaydataGrid.SelectedItem)).banner_id
                                                     select c;
                phoneDisplayTextbox.Clear();
                student.ToList();
                if (student.Count() > 0) //Gets preferred number from the members list if it exists
                {
                    phoneDisplayTextbox.Text = student.First().pphone;
                }
            }
        }

        private void AddDriver_Click(object sender, RoutedEventArgs e) //Add driver
        {
            driverAddedSucessfullyLabel.Visibility = System.Windows.Visibility.Hidden;
            driverAlreadyExistsLabel.Visibility = System.Windows.Visibility.Hidden;
            DataClassesDataContext db = new DataClassesDataContext();
            // Create a new Driver object.
            Driver driver = new Driver
            {
                first_name = firstnameDisplayLabel.Content.ToString(),
                last_name = lastnameDisplayLabel.Content.ToString(),
                EMail = emailDisplayTextbox.Text,
                PhoneNumber = phoneDisplayTextbox.Text,
                password = "cssa"
            };

            // Add the new object to the Orders collection.
            db.Drivers.InsertOnSubmit(driver);

            IQueryable<Driver> priordriver = from c in db.Drivers
                                             where c.first_name == driver.first_name && c.last_name == driver.last_name
                                             select c;
            priordriver.ToList();
            // Submit the change to the database.

            if (priordriver.Count() == 0) //if the driver does not already exist
            {
                try
                {
                    db.SubmitChanges();
                    driverAddedSucessfullyLabel.Visibility = System.Windows.Visibility.Visible;
                }
                catch (Exception c)
                {
                    Console.WriteLine(c);
                    // Make some adjustments.
                    // ...
                    // Try again.
                    db.SubmitChanges();
                }
            }
            else
            {
                driverAlreadyExistsLabel.Visibility = System.Windows.Visibility.Visible;
            }
        }


    }
}
